package com.longcai.cm.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.longcai.cm.common.utils.StringUtil;
import com.longcai.cm.dao.JaMemberAssetMapper;
import com.longcai.cm.dao.JaMemberLogIntegralMapper;
import com.longcai.cm.domain.JaMemberAsset;
import com.longcai.cm.domain.JaMemberLogIntegral;
import com.longcai.cm.exception.SysRuntimeException;
import com.longcai.cm.service.JaMemberAssetService;
import com.longcai.cm.base.service.BaseServiceImpl;
import org.springframework.stereotype.Service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.math.BigDecimal;


/**
 * @author LBX
 */
@Service
public class JaMemberAssetServiceImpl extends BaseServiceImpl<JaMemberAsset> implements JaMemberAssetService {

    @Resource
    private JaMemberAssetMapper memberAssetMapper;

    @Resource
    private JaMemberLogIntegralMapper memberLogIntegralMapper;

    @Override
    public JaMemberAsset createAccount(String memberId) {
        JaMemberAsset memberAsset = new JaMemberAsset();
        memberAsset.setId(StringUtil.getUuid());
        memberAsset.setMemberId(memberId);
        memberAsset.setCreateTime(DateUtil.now());
        memberAsset.setIntegral(new BigDecimal(0));
        memberAssetMapper.insert(memberAsset);
        return memberAsset;
    }

    @Override
    public JaMemberAsset getMemberAsset(String memberId) {
        JaMemberAsset asset = lambdaQuery().eq(JaMemberAsset::getMemberId, memberId).one();
        if (asset == null) {
            asset = createAccount(memberId);
        }
        return asset;
    }

    /**
     * 更新账户 积分信息
     *
     * @param memberId 用户id
     * @param assets   变动量
     * @param type     变动类型( 0: 减少 1:增加  )
     * @param describe 变动描述
     */
    @Override
    public void updateMemberAssetIntegral(String memberId, BigDecimal assets, int type, String describe) {
        if (assets.doubleValue() <= 0) {
            return;
        }
        final JaMemberAsset asset = getMemberAsset(memberId);
        final BigDecimal result;
        if (type == 0) {
            result = asset.getIntegral().subtract(assets);
            if (result.doubleValue() < 0) {
                throw new SysRuntimeException("积分余额不足");
            }
        } else {
            result = asset.getIntegral().add(assets);
        }
//        创建账户明细
        createMemberLogIntegral(memberId, assets, type, result, asset.getIntegral(), describe);
        asset.setIntegral(result);
        memberAssetMapper.updateById(asset);
    }

    @Override
    public void removeRecord(String orderNum) {
        memberLogIntegralMapper.delete(
                new LambdaQueryWrapper<JaMemberLogIntegral>()
                        .like(JaMemberLogIntegral::getLogDescribe, orderNum)
        );
    }

    /**
     * 更新账户积分明细
     *
     * @param memberId  用户id
     * @param changeNum 变动额
     * @param type      变动类型( 0: 减少 1:增加  )
     * @param result    最终额
     * @param origin    原有额
     * @param describe  变动描述
     */
    private void createMemberLogIntegral(String memberId, BigDecimal changeNum, Integer type, BigDecimal result, BigDecimal origin, String describe) {
        JaMemberLogIntegral integral = new JaMemberLogIntegral();
        integral.setId(StringUtil.getUuid());
        integral.setMemberId(memberId);
        integral.setChangeNum(changeNum);
        integral.setLogType(type);
        integral.setResultNum(result);
        integral.setCreateTime(DateUtil.now());
        integral.setOriginalNum(origin);
        integral.setLogDescribe(describe);
        memberLogIntegralMapper.insert(integral);
    }

}
